package com.kgc.personprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.personcommon.pojo.Permission;
import com.kgc.personprovider.dao.PermissionMapper;
import com.kgc.personprovider.service.PermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements PermissionService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public ResponseMsg getAll(Map<String, Object> params) {
        logger.info("PermissionServiceImpl getAll, params: {}", params);
        ResponseMsg msg = new ResponseMsg();
        List<Permission> list = permissionMapper.getAllPermissions(params);
        logger.debug("PermissionServiceImpl getAll, list: {}", list+",params"+params);
        if (list != null && !list.isEmpty()) {
            msg.setCode("200");
            msg.setData(list);
        } else {
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg getById(String id) {
        logger.info("PermissionServiceImpl getById, id: {}", id);
        ResponseMsg msg = new ResponseMsg();
        Permission p = permissionMapper.getPermissionById(Integer.valueOf(id));
        logger.debug("PermissionServiceImpl getById, p: {}", p+",id"+id);
        if (p != null) {
            msg.setCode("200");
            msg.setData(p);
        } else {
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg getByPage(Map<String, Object> params) {
        logger.info("PermissionServiceImpl getByPage, params: {}", params);
        ResponseMsg msg = new ResponseMsg();
        int pageNow  = Integer.parseInt(params.get("pageNow").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNow, pageSize);
        List<Permission> list = permissionMapper.getAll(params);
        logger.debug("PermissionServiceImpl getByPage, list: {}", list+",params"+params);
        PageHelper.clearPage();
        PageInfo<Permission> pageInfo = new PageInfo<>();
        int total = permissionMapper.getCount(params);
        logger.debug("PermissionServiceImpl getByPage, total: {}", total+",params"+params);
        if (list != null && !list.isEmpty()) {
            pageInfo.setList(list);
            pageInfo.setPageNo(pageNow);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTotal((int) total);
        }

        if (pageInfo.getList() != null && !pageInfo.getList().isEmpty()) {
            msg.setCode("200");
            msg.setData(pageInfo);
        } else {
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg add(Permission permission) {
        logger.info("PermissionServiceImpl add, permission: {}", permission);
        ResponseMsg msg = new ResponseMsg();
        Date now = new Date();
//        permission.setCreateUser("admin");
        permission.setCreateDate(now);
//        permission.setUpdateUser("admin");
        permission.setUpdateDate(now);
        permission.setIs_delete(0);
        int cnt = permissionMapper.addPermission(permission);
        logger.debug("PermissionServiceImpl add, cnt: {}", cnt);
        msg.setCode(cnt > 0 ? "200" : "201");
        return msg;
    }

    @Override
    public ResponseMsg update(Permission permission) {
        logger.info("PermissionServiceImpl update, permission: {}", permission);
        ResponseMsg msg = new ResponseMsg();
//        permission.setUpdateUser("111");
        permission.setUpdateDate(new Date());
        int cnt = permissionMapper.updatePermission(permission);
        logger.debug("PermissionServiceImpl update, cnt: {}", cnt);
        msg.setCode(cnt > 0 ? "200" : "201");
        return msg;
    }

    @Override
    public ResponseMsg delete(String id) {
        logger.info("PermissionServiceImpl delete, id: {}", id);
        ResponseMsg msg = new ResponseMsg();
        int cnt = permissionMapper.deletePermission(Integer.valueOf(id));
        logger.debug("PermissionServiceImpl delete, cnt: {}", cnt);
        msg.setCode(cnt > 0 ? "200" : "201");
        return msg;
    }

    @Override
    public Permission getPermByPath(String purl) {
        logger.info("PermissionServiceImpl getPermByPath, purl: {}", purl);
        Permission permission = permissionMapper.getPermByPath(purl);
        logger.debug("PermissionServiceImpl getPermByPath, permission: {}", permission);
        return permission;
    }

    @Override
    public boolean checkPermission(String name, String purl) {
        Permission permission = permissionMapper.getPermByPath(purl);
        if (permission != null) {
            if (permission.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }
}
